package com.ivt.service.impl;

import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.ivt.core.ex.IvtException;
import com.ivt.dao.ResourceDao;
import com.ivt.model.Classes;
import com.ivt.model.Resource;
import com.ivt.service.ResourceService;
import com.ivt.utils.enums.AuditStatus;

@Service
public class ResourceServiceImpl implements ResourceService {

	@javax.annotation.Resource
	private ResourceDao resourceDao;
	
	@Override
	public void regist(Resource resource) throws IvtException {
		// TODO Auto-generated method stub

	}

	@Override
	public Resource create(Resource resource) throws IvtException {
		if (null == resource) {
			throw new IvtException("资源信息不能为空");
		}
		if (StringUtils.isBlank(resource.getResourceName())) {
			throw new IvtException("资源名称不能为空");
		}
		if (StringUtils.isBlank(resource.getResourceUrl())) {
			throw new IvtException("资源路径不能为空");
		}

		long  c = resourceDao.existsByName(resource.getResourceName());
		if (0 < c) {
			throw new IvtException("资源【" + resource.getResourceName() + "】已存在");
		}

		resource.setResourceId(null);

		return resourceDao.saveAndFlush(resource);
	}

	@Override
	public Resource update(Resource resource) throws IvtException {
		if (null == resource || null == resource.getResourceId()
				|| 0 >= resource.getResourceId()) {
			throw new IvtException("班级信息不能为空");
		}
		if (StringUtils.isBlank(resource.getResourceName())
				|| StringUtils.isBlank(resource.getResourceUrl())) {
			throw new IvtException("参数错误,没有要修改的项目");
		}

		if (StringUtils.isNotBlank(resource.getResourceName())
				&& 0 < resourceDao.existsByNameAndId(resource.getResourceName(),
						resource.getResourceId())) {
			throw new IvtException("班级【" + resource.getResourceName() + "】已存在");
		}

		return resourceDao.saveAndFlush(resource);

	}

	@Override
	public void delete(Resource resource) throws IvtException {
		// TODO Auto-generated method stub

	}

	@Override
	public Resource delete(Long resourceId) throws IvtException {
		if (null == resourceId || 0 >= resourceId) {
			throw new IvtException("参数错误");
		}

		Resource resource = new Resource();
		resource.setResourceId(resourceId);
//		resource.setStatus(AuditStatus.NOT);
		return resourceDao.saveAndFlush(resource);

	}

	@Override
	public Resource getResourca(Long resourceId) throws IvtException {
		if (null == resourceId || 0 >= resourceId) {
			throw new IvtException("参数错误");
		}
		return resourceDao.findOne(resourceId);
	}

	@Override
	public List<Resource> getResourca(Set<Long> resourceId) throws IvtException {
		// TODO Auto-generated method stub
		return null;
	}

}
